reBox = document.querySelector('#regin')
reId = reBox.querySelector('.reId')
reUser = reBox.querySelector('.reUser')
rePass = reBox.querySelector('.rePass')
reSeclectPass = reBox.querySelector('.selectPass')
reBtn = reBox.querySelector('.reBtn')
reP = reBox.querySelector('.reg_p3')
rePipt = reP.querySelector('input')
reEm = reP.querySelector('em')
reI = reBox.querySelectorAll('.reI')
aI = reBox.querySelectorAll('i')

// 正则验证
var reTest = [
    /^[1][3,4,5,7,8][0-9]{9}$/,
    /^\w{1,6}$/,
    /^\w{6,16}$/
]
// input框取消聚焦开始验证
function re(reData, i, a) {
    if (reData.test(a)) {
        aI[i].innerHTML = '✔'
        aI[i].className = 'testGreen'
        
    } else {
        aI[i].innerHTML = 'X'
        aI[i].className = 'testRed'
    }
}
reI.forEach(function(ele,index){
    ele.onblur = function(){
        re(reTest[index],index,ele.value)
    }
    console.log()
})

// input 聚焦恢复原来
var iValue = []
aI.forEach(function(ele,index){
    iValue.push(ele.innerHTML)
})
reI.forEach(function(ele,index){
    ele.addEventListener('focus',function(){
        if(this.value === ''){
            aI[index].innerHTML = iValue[index]
            aI[index].className = ''
        }
    })
})


// 判断是否点击用户协议 
rePipt.addEventListener('click', function () {
    console.log(this.checked)
    if (this.checked) {
        reBtn.className = 'reBtn btnBackground'
        reBtn.addEventListener('click', reginFn)
    } else {
        reBtn.className = 'reBtn'
        reBtn.removeEventListener('click', reginFn)
    }
})

// 点击已有帐号  跳转登陆页面
reEm.addEventListener('click',function(){
    location.href = '../html/login.html'
})

function reginFn() {
    if (!reId.value || !reUser.value || !rePass.value || !reSeclectPass.value) {
        alert('帐号密码不能为空')
        return false
    }

    if (rePass.value === reSeclectPass.value) {
        pAjax({
            url: '../server/regin.php',
            type: 'post',
            data: {
                ID: reId.value,
                username: reUser.value,
                password: rePass.value
            }
        }).then(function (res) {
            console.log(res)
            let json = JSON.parse(res)

            if (json.err === 0) {
                alert(json.msg)
                location.href = '../html/login.html'
            } else {
                alert(json.msg)
                location.href = '../html/login.html'
            }
        })
    } else {
        alert('两次密码输入不一致！')
        return false
    }
}

